package com.es.disped.web.controller.oj.solution;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.es.disped.api.oj.solution.HustSolutionService;
import com.es.disped.common.annotation.DynamicDataSource;
import com.es.disped.common.model.MsgModel;
import com.es.disped.common.model.PageModel;
import com.es.disped.core.controller.BaseController;
import com.es.disped.dao.oj.model.Solution;
import com.es.disped.dao.oj.model.SolutionExample;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api("OJ判题结果管理")
@Controller
@RequestMapping("/oj/solution")
public class SolutionController extends BaseController{
	
	private final String NAMESPACE="com.es.disped.dao.mapper.solution.SolutionCustomMapper";

	@Autowired
	HustSolutionService hustSolutionService;
	
	
	@ApiOperation(value="获取用户所有答题结果",response=MsgModel.class,httpMethod="POST",notes="通过用户ID判断用户是否通过答题")
	@RequestMapping(value="/getProblemResultList",method=RequestMethod.POST)
	@DynamicDataSource
	public @ResponseBody MsgModel judgeProblemPassResult(@RequestBody @ApiParam(name="solution",value="判题结果对象",required=true) Solution solution)
	{	
		List<Solution> solutions=null;
		if(!this.isNull(solution.getUserId()))
		{
			solutions=this.getHustService().selectList(NAMESPACE+".getProblemResultList", solution.getUserId());
		}
		Map<Integer, String> map=new HashMap<Integer, String>();
		if(!this.isNull(solutions))
		{
			for(Solution record:solutions)
			{
				map.put(record.getSolutionId(), record.getResult()==(short)4?"Y":"N");
			}
		}
		return this.resultMsg(!this.isNull(solutions)?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				!this.isNull(solutions)?"获取成功":"获取失败",!this.isNull(solutions)?map:"");
	}
	
	
	@ApiOperation(value="获取用户答题情况列表",response=MsgModel.class,httpMethod="POST",notes="获取用户提交记录")
	@RequestMapping(value="/getProblemPassUserList",method=RequestMethod.POST)
	@DynamicDataSource
	public @ResponseBody MsgModel getProblemPassResult(@RequestBody @ApiParam(name="solution",value="判题结果对象",required=false) Solution solution)
	{
		List<Solution> list=null;
		SolutionExample solutionExample=new SolutionExample();
		SolutionExample.Criteria criteria=solutionExample.createCriteria();
		if(!this.isNull(solution.getProblemId()))
		{
			criteria.andProblemIdEqualTo(solution.getProblemId());
		}
		if(!this.isNull(solution.getResult()))
		{
			criteria.andResultEqualTo(solution.getResult());
		}
		if(!this.isNull(solution.getSolutionId()))
		{
			criteria.andSolutionIdEqualTo(solution.getSolutionId());
		}
		if(!this.isNull(solution.getUserId()))
		{
			criteria.andUserIdEqualTo(solution.getUserId());
		}
		solutionExample.setOrderByClause("judgetime desc");
		list=hustSolutionService.selectByExample(solutionExample);
		return this.resultMsg(!this.isNull(list)?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				!this.isNull(list)?"列表加载成功":"数据为空",!this.isNull(list)?list:"");
	}
	
	
	
	@ApiOperation(value="分页获取状态栏目所有答题情况",response=MsgModel.class,httpMethod="POST",notes="分页获取状态栏所有答题情况，分页参数['offset','limit']")
	@RequestMapping(value="/pages",method=RequestMethod.POST)
	@DynamicDataSource
	public @ResponseBody MsgModel pages(@RequestBody @ApiParam(name="paramMap",value="分页参数[offset,limit]",required=true) Map<String, Object> paramMap)
	{
		SolutionExample listExample=new SolutionExample();
		PageModel<Solution> pageModel=null;
		if(Optional.of(paramMap)
				.filter(offset->paramMap.containsKey("offset")!=false)
				.filter(limit->paramMap.containsKey("limit")!=false)
				.isPresent())
		{
			pageModel=hustSolutionService.selectByExampleForOffsetPage(listExample,
					Integer.valueOf(paramMap.get("offset").toString()), Integer.valueOf(paramMap.get("limit").toString()));
			
		}
		return this.resultMsg(!this.isNull(pageModel)?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				!this.isNull(pageModel)?"判题结果加载成功":"判题结果加载失败",!this.isNull(pageModel)?pageModel:"");
	}
	
//	@ApiOperation(value="获取编译结果",response=MsgModel.class,httpMethod="POST",notes="通过判题ID获取题目编译结果")
//	@RequestMapping(value="/getCompileRuntimeInfo",method=RequestMethod.POST)
//	@DynamicDataSource
//	public @ResponseBody MsgModel getSource(@RequestBody Solution solution)
//	{
//		if(Optional.of(solution)
//				.filter(sId->solution.getSolutionId()!=null)
//				.filter(uId->solution.getUserId()!=null)
//				.isPresent())
//		{
//			SolutionExample selectExample=new SolutionExample();
//			SolutionExample.Criteria criteria=selectExample.createCriteria();
//			criteria.andSolutionIdEqualTo(solution.getSolutionId()).andUserIdEqualTo(solution.getUserId());
//			selectExample.ord
//			Solution res=hustSolutionService.selectByExample(selectExample);
//		}
//		return this.resultMsg(null);
//	}
	
//	@ApiOperation(value="AAAAAAAAAAAA",response=MsgModel.class,httpMethod="POST",
//			notes="AAAAAAAAAAAA")
//	@RequestMapping(value="/a",method=RequestMethod.POST)
//	@DynamicDataSource
//	public @ResponseBody MsgModel getUserACRate(@RequestBody Integer problemId)
//	{
////		List<Solution> list=null;
//		List<ProblemPassResult> list=this.getHustService().selectList("com.es.disped.dao.mapper.problem.ProblemCustomMapper.getProblemPassResult", problemId);
//		return this.resultMsg(HttpStatus.OK.value(),"",list);
//	}
}
